<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="链接地址" prop="link">
        <el-input
          v-model="queryParams.link"
          placeholder="请输入链接地址"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="一级服务类别" prop="serviceType">
        <el-select
          v-model="queryParams.serviceType"
          placeholder="请选择一级服务类别"
          clearable
          size="small"
          @change="selectSubclassOptions"
        >
          <el-option
            v-for="dict in serviceTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="二级服务类别" prop="subclass">
        <el-select
          v-model="queryParams.subclass"
          placeholder="请选择二级服务类别"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in subclassOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select
          v-model="queryParams.type"
          placeholder="请选择类型"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in typeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in serviceStatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="说明" prop="remark">
        <el-input
          v-model="queryParams.remark"
          placeholder="请输入说明"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="cyan"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:room:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:room:remove']"
          >删除</el-button
        >
      </el-col>
      <!--
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:room:export']"
        >导出</el-button>
      </el-col>
      -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="roomList"
      @selection-change="handleSelectionChange"
      :default-sort="{ prop: 'createTime', order: 'descending' }"
      @sort-change="tableSortChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="名称"
        align="center"
        prop="name"
        :sort-orders="['ascending', 'descending']"
        sortable="custom"
      />
      <el-table-column
        label="链接地址"
        align="center"
        prop="link"
        :sort-orders="['ascending', 'descending']"
        sortable="custom"
      />
      <el-table-column
        label="一级服务类别"
        align="center"
        prop="serviceType"
        :formatter="serviceTypeFormat"
        width="100"
      />
      <el-table-column label="二级服务类别"  width="100" align="center" prop="subclassText" />
      <el-table-column
        label="类型"
        align="center"
        prop="type"
        :formatter="typeFormat"
        width="80"
      />
      <el-table-column label="图标" align="center" prop="picture" width="80">
        <template slot-scope="scope">
          <img :src="scope.row.picture" alt="" width="30" height="30" />
        </template>
      </el-table-column>
      <el-table-column
        label="状态"
        align="center"
        prop="status"
        :formatter="serviceStatusFormat"
        width="80"
      />
      <el-table-column
        label="排序"
        align="center"
        prop="num"
        :sort-orders="['ascending', 'descending']"
        sortable="custom"
        width="80"
      />
      <el-table-column
        label="说明"
        align="center"
        prop="remark"
        :sort-orders="['ascending', 'descending']"
        sortable="custom"
      />
      <el-table-column
        label="操作"
        align="center"
        width="80"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:room:edit']"
            >修改</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改服务大厅对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择类型" @change="statusChange">
            <el-option
              v-for="dict in typeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            >
            </el-option>
          </el-select>
        </el-form-item>
         <el-form-item label="附件" prop="filePath" :hidden="!linkDisable">
          <el-upload
            ref="upload"
            :limit="1"
            :on-exceed="handleExceedFile"
            :action="uploadFile.url"
            :headers="uploadFile.headers"
            :file-list="uploadFile.fileList"
            :on-progress="handleFileUploadProgressFile"
            :on-success="handleFileSuccessFile"
            :auto-upload="true"
            :on-preview="handlePreviewFile"
            :on-remove="handleRemoveFile"
            :multiple="false"
            :before-remove="beforeRemoveFile"
          >
            <el-button slot="trigger" size="small" type="primary"
              >选取文件</el-button
            >
           <!-- 
             <div slot="tip" class="el-upload__tip">
              只能上传jpg/png文件，且不超过500kb
            </div>
            -->
          </el-upload>
        </el-form-item>
        <el-form-item label="链接地址" prop="link">
          <el-input type="textarea" autosize="{ minRows: 1, maxRows: 6 }" v-model="form.link" :disabled="linkDisable" placeholder="请输入链接地址" />
        </el-form-item>
        <el-form-item label="一级服务类别" prop="serviceType">
          <el-select
            v-model="form.serviceType"
            placeholder="请选择一级服务类别"
            @change="selectSubclassOptions1"
          >
            <el-option
              v-for="dict in serviceTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="二级服务类别" prop="subclass">
          <el-select v-model="form.subclass" placeholder="请选择二级服务类别">
            <el-option
              v-for="dict in subclassOptions1"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="图片地址" prop="picture">
          <el-upload
            ref="upload"
            :limit="1"
            :on-exceed="handleExceed"
            accept=".jpg, .png"
            :action="upload.url"
            :headers="upload.headers"
            :file-list="upload.fileList"
            :on-progress="handleFileUploadProgress"
            :on-success="handleFileSuccess"
            :auto-upload="true"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :multiple="false"
            :before-remove="beforeRemove"
          >
            <el-button slot="trigger" size="small" type="primary"
              >选取文件</el-button
            >
            <div slot="tip" class="el-upload__tip">
              只能上传jpg/png文件，且不超过500kb
            </div>
          </el-upload>
        </el-form-item>

        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option
              v-for="dict in serviceStatusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="排序" prop="num">
          <el-input-number
            :precision="2"
            :step="0.1"
            v-model="form.num"
            placeholder="请输入排序"
          />
        </el-form-item>

        <el-form-item label="说明" prop="remark">
          <el-input
            type="textarea"
            autosize="{ minRows: 1, maxRows: 6 }"
            v-model="form.remark"
            placeholder="请输入说明"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listRoom,
  getRoom,
  delRoom,
  addRoom,
  updateRoom,
  exportRoom,
} from "@/api/system/room";
import { getToken } from "@/utils/auth";
export default {
  name: "Room",
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      names:[],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      subclassOptions: [],
      subclassOptions1: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 服务大厅表格数据
      roomList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 一级服务类别字典
      serviceTypeOptions: [],
      serviceStatusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        remark: null,
        serviceType: null,
        subclass: null,
        type: null,
        link: null,
        status,
      },
      // 表单参数
      form: {},
      typeOptions: [],
      // 表单校验
      rules: {
        name: [{ required: true, message: "名称不能为空", trigger: "blur" }],
        link: [
          { required: true, message: "链接地址不能为空", trigger: "blur" },
        ],
        serviceType: [
          { required: true, message: "请选择一级服务类别", trigger: "change" },
        ],
        subclass: [
          { required: true, message: "请选择二级服务类别", trigger: "change" },
        ],
        type: [{ required: true, message: "请选择类型", trigger: "change" }],
        num: [{ required: true, message: "排序不能为空", trigger: "blur" }],
        status: [{ required: true, message: "请选择状态", trigger: "change" }],
      },
      // 上传参数
      upload: {
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/common/upload",
        // 上传的文件列表
        fileList: [],
        file: null,
      },
      uploadFile: {
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/common/upload",
        // 上传的文件列表
        fileList: [],
        file: null,
      },
      linkDisable:false

    };
  },
  created() {   
    this.getDicts("service_type").then((response) => {
      this.serviceTypeOptions = response.data;
    });
    this.getDicts("service_status").then((response) => {
      this.serviceStatusOptions = response.data;
    });
    this.getDicts("service_link_type").then((response) => {
      this.typeOptions = response.data;
    });

    this.getList();
  },
  methods: {
    statusChange(val){
      if(val && (val==="download_link" || val==="preview_link") ){
        this.linkDisable = true;
        this.form.link = undefined;
      }else{
        this.linkDisable = false;
      }
    },
    selectSubclassOptions(val) {
      this.getSubclassOptions(val);
    },
    selectSubclassOptions1(val) {
      this.getSubclassOptions1(val);
    },
    getSubclassOptions(data) {
      if (data) {
        this.getDicts(data).then((response) => {
          this.subclassOptions = response.data;
        });
      } else {
        this.subclassOptions = [];
      }
    },
    getSubclassOptions1(data) {
      if (data) {
        this.getDicts(data).then((response) => {
          this.subclassOptions1 = response.data;
        });
      } else {
        this.subclassOptions1 = [];
      }
    },
    /** 查询服务大厅列表 */
    getList() {
      this.loading = true;
      listRoom(this.queryParams).then((response) => {
        this.roomList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 一级服务类别字典翻译
    serviceTypeFormat(row, column) {
      return this.selectDictLabel(this.serviceTypeOptions, row.serviceType);
    },
    serviceStatusFormat(row, column) {
      return this.selectDictLabel(this.serviceStatusOptions, row.status);
    },
    typeFormat(row, column) {
      return this.selectDictLabel(this.typeOptions, row.type);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        guid: null,
        name: null,
        remark: null,
        serviceType: null,
        subclass: null,
        type: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        link: null,
        picture: null,
        status: null,
        num: 0,
      };
      this.upload.fileList=[];
      this.linkDisable = false;
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.guid);
      this.names = selection.map((item) => item.name);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加服务大厅";
      this.subclassOptions1 = [];
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const guid = row.guid || this.ids;
      getRoom(guid).then((response) => {
        this.form = response.data;
        if(this.form && this.form.type && (this.form.type==="download_link" || this.form.type==="preview_link") ){
          this.linkDisable = true;
        }else{
          this.linkDisable = false;
        }

        this.open = true;
        this.title = "修改服务大厅";
        this.getSubclassOptions1(this.form.serviceType);
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.guid != null) {
            updateRoom(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRoom(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const guids = row.guid || this.ids;
      const names = row.name || this.names;
      this.$confirm(
        '是否确认删除服务大厅名称为"' + names + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delRoom(guids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有服务大厅数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportRoom(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
        });
    },
    tableSortChange(column) {
      this.queryParams.pageNum = 1;
      if (column.order === "descending") {
        this.queryParams.sortBy = column.prop;
        this.queryParams.order = "desc";
      } else {
        this.queryParams.sortBy = column.prop;
        this.queryParams.order = "asc";
      }
      this.getList();
    },
    // 选择时事件提醒
    handleExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${
          files.length + fileList.length
        } 个文件,请删除后重新下载`
      );
    },
    handleExceedFile(files, fileList) {
      this.$message.warning(
        `当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${
          files.length + fileList.length
        } 个文件,请删除后重新下载`
      );
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    handleFileUploadProgressFile(event, file, fileList) {
      this.uploadFile.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.isUploading = false;
      this.form.picture = response.url;
      this.msgSuccess(response.msg);
      fileList = [];
    },
    handleFileSuccessFile(response, file, fileList) {
      this.uploadFile.isUploading = false;
      this.form.link = response.url;
      this.msgSuccess(response.msg);
      fileList = [];
    },
    // 文件列表移除文件时的钩子
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handleRemoveFile(file, fileList) {
      console.log(file, fileList);
    },
    // 点击文件列表中已上传的文件时的钩子
    handlePreview(file) {
      console.log(file);
    },
    handlePreviewFile(file) {
      console.log(file);
    },
    // 移出前事件提示
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`);
    },
    beforeRemoveFile(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`);
    },
  },
};
</script>
